package com.xy.p2p.action.admin;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.model.Tuser;
import com.xy.p2p.service.TmemberassetaccountService;



@Controller
public class TmemberassetaccountAction{
	private static final Logger log = LoggerFactory.getLogger(TmemberassetaccountAction.class);
	public TmemberassetaccountAction() {
	}
 
 
 	/**
	 * 会员资产账户信息表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmemberassetaccount", new Tmemberassetaccount());
        setDict(model);
		return "tmemberassetaccountEdit";
	}

	/**
	 * 会员资产账户信息表新增保存
	 * 
	 * @param tmemberassetaccount
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tmemberassetaccountSave.do")
	public String save(Tmemberassetaccount tmemberassetaccount, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tmemberassetaccount, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmemberassetaccount = tmemberassetaccountService.save(tmemberassetaccount);
		return "redirect:v_tmemberassetaccountList.do";
	}
	
	@RequestMapping("o_tmemberassetaccountModifyAccount.do")
	public String ModifyAccount(double money,String MOremark,String opr,Tmemberassetaccount tmemberassetaccount, HttpServletRequest request, ModelMap model) {
		tmemberassetaccount = tmemberassetaccountService.get(tmemberassetaccount.getMaaid());
		Tuser tuser = (Tuser)request.getSession().getAttribute(TuserAction.AUTH_KEY);
		if(null== tmemberassetaccountService.ModifyAccount(tmemberassetaccount, opr, tuser, money, MOremark)){
			model.addAttribute("MSG","调账失败");
			return "v_tmemberassetaccountList.do";
		}
		return "redirect:v_tmemberassetaccountList.do";
	}


	/**
	 * 会员资产账户信息表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tmemberassetaccountDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tmemberassetaccountService.remove(id);
			}
		}
		return "redirect:v_tmemberassetaccountList.do";
	}

	/**
	 * 会员资产账户信息表编辑页面
	 * 
	 * @param maaid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountEdit.do")
	public String edit(long maaid, HttpServletRequest request, ModelMap model) {
		Tmemberassetaccount tmemberassetaccount = tmemberassetaccountService.get(maaid);
		model.addAttribute("tmemberassetaccount", tmemberassetaccount);
        setDict(model);
		return "tmemberassetaccountEdit";
	}

	/**
	 * 会员资产账户信息表详情
	 * 
	 * @param maaid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountDetail.do")
	public String detail(long maaid, HttpServletRequest request, ModelMap model) {
		Tmemberassetaccount tmemberassetaccount = tmemberassetaccountService.get(maaid);
		model.addAttribute("tmemberassetaccount", tmemberassetaccount);
        setDict(model);
		return "tmemberassetaccountView";
	}

	/**
	 * 会员资产账户信息表查询（分页）
	 * 
	 * @param tmemberassetaccount
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountList.do")
	public String listPage(Tmemberassetaccount tmemberassetaccount, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmemberassetaccountService.getTmemberassetaccountByPage(toMap(tmemberassetaccount),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tmemberassetaccountList";
	}

	/**
	 * 会员资产账户信息表查询
	 * 
	 * @param tmemberassetaccount
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountList1.do")
	public String list(Tmemberassetaccount tmemberassetaccount, HttpServletRequest request, ModelMap model) {
		List<Tmemberassetaccount> list = tmemberassetaccountService.getTmemberassetaccount(toMap(tmemberassetaccount));
		model.addAttribute("list", list);
        setDict(model);
		return "tmemberassetaccountList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmemberassetaccount
	 * @return
	 */	private Map<String,Object> toMap(Tmemberassetaccount tmemberassetaccount){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tmemberassetaccount.getMaaid()!=null&&!tmemberassetaccount.getMaaid().equals("")){
			map.put("maaid", tmemberassetaccount.getMaaid());
		}
		if(tmemberassetaccount.getTmember()!=null&&!tmemberassetaccount.getTmember().equals("")){
			map.put("Tmember", tmemberassetaccount.getTmember());
		}
		if(tmemberassetaccount.getAmountfrozen()!=null&&!tmemberassetaccount.getAmountfrozen().equals("")){
			map.put("amountfrozen", tmemberassetaccount.getAmountfrozen());
		}
		if(tmemberassetaccount.getAfreason()!=null&&!tmemberassetaccount.getAfreason().equals("")){
			map.put("afreason", tmemberassetaccount.getAfreason());
		}
		if(tmemberassetaccount.getBalance()!=null&&!tmemberassetaccount.getBalance().equals("")){
			map.put("balance", tmemberassetaccount.getBalance());
		}
		if(tmemberassetaccount.getStatus()!=null&&!tmemberassetaccount.getStatus().equals("")){
			map.put("status", tmemberassetaccount.getStatus());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TMEMBERASSETACCOUNT_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmemberassetaccount tmemberassetaccount, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TmemberassetaccountService tmemberassetaccountService;   
}